********************************************************************************
*                                                                              *
*             WORKER-LEVEL ANALYSES OF THE IMPACT OF ROBOT ADOPTION            *
*                                                                              *
********************************************************************************

cd "H:\"

// Variable list for fixed effects regressions
global feweightvar hoursworked09
global fevar sbi4#fyear mun#fyear 
global fevarcompete sbi2#fyear mun#fyear 
global xvar age1824 age2534 age3544 age4554 age5567 hh_single hh_other
global xvaradd male migrant migrant_2ndgen
global cluster beidXyear rinpersoon
global clustercompete industry_ifrXyear rinpersoon

// What do we do below:
*1) Analysis for full sample on hourly wage, hours worked, and employment
*2) Event studies
*3) Analysis by worker type, hourly wage
*4) Analyse worker sorting into robot-adopting firms
*5) Analysis by worker type, hours workerd and employment
*6) Extended event studies
*7) Long differences
*8) Analysis by demographic characteristics
*9) Control for interactions with demographic characteristics
*10) Narrower matching window with LFS
*11) Workers without occupation change
*12) Existing workers/stayers
*13) Job mobility between firms
*14) Medium and high-skilled workers
*15) Personal income

////////////////////////////////////////////////////////////////////////////////
/////////////////////1) WORKER-LEVEL ANALYSIS -- MAIN EFFECTS///////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table 2

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	keep if hwweight == 1
	sort rinpersoon year
	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid year)
	keep if include==1 & include_post2009==1
	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee ==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)
	egen industry_ifrXyear = group(industry_ifr year)

reghdfe loghwage robots [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl1
reghdfe loghwage robots $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl2
reghdfe loghwage robots $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl3
reghdfe empl robots [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl4
reghdfe empl robots $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl5
reghdfe empl robots $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl6	
reghdfe loghoursworked robots [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl7
reghdfe loghoursworked robots $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl8
reghdfe loghoursworked robots $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl9		

qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl9
	outreg2 using "Robots and Workers\Results\Worker_baseline", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
		


		
////////////////////////////////////////////////////////////////////////////////
/////////////////2) WORKER-LEVEL ANALYSIS EVENT STUDIES/////////////////////////
////////////////////////////////////////////////////////////////////////////////			

* Create Figure 4	

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	sort rinpersoon year


	// Do the other selections 
	keep if hwweight == 1
	sort rinpersoon year
	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid year)
	keep if include==1 & include_post2009==1
	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee ==.)
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)
	egen industry_ifrXyear = group(industry_ifr year)

	// Obtain event study variables
	merge m:1 beid year using "Robots and Workers\Data\BEID_PS_20002020.dta", nogen keep(1 3 4 5) keepusing(robots*)
	g robots_gttmin3 = 1-robots_tmin3-robots_tmin2-robots_tmin1-robots

	reghdfe loghwage robots_gttmin3 robots_tmin3 robots_tmin1 robots_t0 robots_gte1 $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
		estimates store repl2
	reghdfe loghwage robots_gttmin3 robots_tmin3 robots_tmin1 robots_t0 robots_gte1 $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
		estimates store repl3

	// Export output
	global sheetname EventStudies_workers_2  

	//// Baseline results
	putexcel set "Robots and Workers\Results\Figures.xlsx", modify sheet($sheetname)
	putexcel A1 = "N=1,131,970"
	putexcel A4 = "Worker level results"
	putexcel A5 = "Years before/after robot adoption"
	putexcel A6 = -5
	putexcel A7 = -5
	putexcel A8 = -4
	putexcel A9 = -4
	putexcel A10 = -3
	putexcel A11 = -3
	putexcel A12 = -2
	putexcel A13 = -2
	putexcel A14 = -1
	putexcel A15 = -1
	putexcel A16 = 0
	putexcel A17 = 0
	putexcel A18 = 1	
	putexcel A19 = 1	
	putexcel A20 = 2	
	putexcel A21 = 2

	local i = 1
	foreach spec in 2 3 {
		estimates restore repl`spec'
		local letter = char(65+`i')
		putexcel `letter'5 = "Specification `spec'"
		putexcel `letter'6 = .
		putexcel `letter'7 = .
		putexcel `letter'8 = _b[robots_gttmin3]
		putexcel `letter'9 = _b[robots_gttmin3]
		putexcel `letter'10 = _b[robots_tmin3]
		putexcel `letter'11 = _b[robots_tmin3]
		putexcel `letter'12 = 0
		putexcel `letter'13 = 0
		putexcel `letter'14 = _b[robots_tmin1]
		putexcel `letter'15 = _b[robots_tmin1]
		putexcel `letter'16 = _b[robots_t0]
		putexcel `letter'17 = _b[robots_t0]
		putexcel `letter'18 = _b[robots_gte1]
		putexcel `letter'19 = _b[robots_gte1]
		putexcel `letter'20 = .
		putexcel `letter'21 = .	
		local letter = char(66+`i')
		putexcel `letter'6 = .
		putexcel `letter'7 = .
		putexcel `letter'8 = _b[robots_gttmin3]-1.96*_se[robots_gttmin3]
		putexcel `letter'9 = _b[robots_gttmin3]+1.96*_se[robots_gttmin3]
		putexcel `letter'10 = _b[robots_tmin3]-1.96*_se[robots_tmin3]
		putexcel `letter'11 = _b[robots_tmin3]+1.96*_se[robots_tmin3]
		putexcel `letter'12 = 0
		putexcel `letter'13 = 0
		putexcel `letter'14 = _b[robots_tmin1]-1.96*_se[robots_tmin1]
		putexcel `letter'15 = _b[robots_tmin1]+1.96*_se[robots_tmin1]
		putexcel `letter'16 = _b[robots_t0]-1.96*_se[robots_t0]
		putexcel `letter'17 = _b[robots_t0]+1.96*_se[robots_t0]
		putexcel `letter'18 = _b[robots_gte1]-1.96*_se[robots_gte1]
		putexcel `letter'19 = _b[robots_gte1]+1.96*_se[robots_gte1]
		putexcel `letter'20 = .
		putexcel `letter'21 = .
		local i = `i'+2
	}
		

	
////////////////////////////////////////////////////////////////////////////////
//////////////////////3) WORKER-LEVEL ANALYSIS BY WORKER TYPE///////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table 3

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl1
reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl2
reghdfe loghwage                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl3	
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl4
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl5
reghdfe loghwage                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl6
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl7
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl8
reghdfe loghwage                	  robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl9
		
	
qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl9
	outreg2 using "Robots and Workers\Results\Worker_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
		
	
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////4) WORKER SORTING/////////////////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table 4

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	merge m:1 rinpersoon year using "Robots and Workers\Data\Workers_PS_LFS_jobchanges.dta", nogen keep(1 3)	
	merge m:1 beid year using "Robots and Workers\Data\BEID_PS_20002020.dta", nogen keep(1 3 4 5) keepusing(robots_*0 robots_*1)

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

	g robotsfirmXmeffect = robotsfirm*meffect
	g robotsfirmXrepl = robotsfirm*replaceable
	g robotsfirmXlowskilled = robotsfirm*lowskilled
	g robotsfirmXnonmeffect = robotsfirm*(1-meffect)
	g robotsfirmXnonrepl = robotsfirm*(1-replaceable)
	g robotsfirmXnonlowskilled = robotsfirm*(1-lowskilled)

	// Obtain fixed effects
	reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(firmfe1=beid $fevar workerfe1=rinpersoon) cluster($cluster), if manufacturing==1
		estimates store repl_fe1
	reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(firmfe2=beid $fevar workerfe2=rinpersoon) cluster($cluster), if manufacturing==1
		estimates store repl_fe2
	reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(firmfe3=beid $fevar workerfe3=rinpersoon) cluster($cluster), if manufacturing==1
		estimates store repl_fe3
		
	reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect robotsfirmXmeffect $xvar [weight=indweight], absorb(firmfe4=beid $fevar workerfe4=rinpersoon) cluster($cluster), if manufacturing==1
		estimates store repl2_fe1
	reghdfe loghwage robotsXnonrepl robotsXrepl replaceable robotsfirmXrepl $xvar [weight=indweight], absorb(firmfe5=beid $fevar workerfe5=rinpersoon) cluster($cluster), if manufacturing==1
		estimates store repl2_fe2
	reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled robotsfirmXlowskilled $xvar [weight=indweight], absorb(firmfe6=beid $fevar workerfe6=rinpersoon) cluster($cluster), if manufacturing==1
		estimates store repl2_fe3

		preserve 
			keep year rinpersoon firmfe* workerfe*
			save "Robots and Workers\Data\temp_workerfe.dta", replace
		restore
	merge 1:1 rinpersoon year using "Robots and Workers\Data\temp_workerfe.dta", nogen keep(1 3)


	corr firmfe1 workerfe1
	corr firmfe2 workerfe2
	corr firmfe3 workerfe3

	foreach var in newhire leaver {
		g robotsXnonmeffectX`var' = robotsXnonmeffect*`var'
		g robotsXmeffectX`var' = robotsXmeffect*`var'
		
	}

	by rinpersoon: egen robotsworker = max(robots)

reghdfe workerfe1 robotsXnonmeffect robotsXmeffect robotsfirmXnonmeffect robotsfirmXmeffect meffect $xvar $xvaradd [weight=indweight], absorb($fevar) cluster($cluster), if manufacturing==1 
	estimates store repl_split_1		
reghdfe workerfe1 robotsXnonmeffect robotsXmeffect robotsfirmXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 
	estimates store repl_split_2		
reghdfe workerfe1 robotsXnonmeffect robotsXmeffect robotsfirmXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & robots==0	
	estimates store repl_split_3	
reghdfe workerfe1 robotsXnonmeffect robotsXmeffect robotsfirmXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & newhire==1
	estimates store repl_split_4
reghdfe workerfe1 robotsXnonmeffect robotsXmeffect robotsfirmXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & leaver==1
	estimates store repl_split_5
	
	
qui estimates restore repl_split_1
qui outreg2 using "Robots and Workers\Results\Worker_sorting_hwage_bluecollarroutine", label aster replace word tex ctitle(Worker fixed effects) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl_split_2
qui outreg2 using "Robots and Workers\Results\Worker_sorting_hwage_bluecollarroutine", label aster append word tex ctitle(Worker fixed effects) nocon
qui estimates restore repl_split_3
qui outreg2 using "Robots and Workers\Results\Worker_sorting_hwage_bluecollarroutine", label aster append word tex ctitle(Worker fixed effects) nocon
qui estimates restore repl_split_4
qui outreg2 using "Robots and Workers\Results\Worker_sorting_hwage_bluecollarroutine", label aster append word tex ctitle(Worker fixed effects) nocon
qui estimates restore repl_split_5
    outreg2 using "Robots and Workers\Results\Worker_sorting_hwage_bluecollarroutine", label aster append word tex ctitle(Worker fixed effects) nocon	

	
////////////////////////////////////////////////////////////////////////////////
//////////3) WORKER-LEVEL ANALYSIS BY WORKER TYPE -- EMPLOYMENT EFFECTS/////////
////////////////////////////////////////////////////////////////////////////////		
	
* Create Table 5

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)	
		
	
reghdfe loghoursworked robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl61
reghdfe loghoursworked robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl62
reghdfe loghoursworked                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl63	
reghdfe loghoursworked robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl64
reghdfe loghoursworked robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl65
reghdfe loghoursworked                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl66
reghdfe loghoursworked robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl67
reghdfe loghoursworked robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl68
reghdfe loghoursworked                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl69

qui estimates restore repl61
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster replace word tex ctitle(Hours worked \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl62
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl63
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl64
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl65
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl66
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl67
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl68
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl69
	outreg2 using "Robots and Workers\Results\Worker_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon		

reghdfe empl robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl81
reghdfe empl robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl82
reghdfe empl                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl83	
reghdfe empl robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl84
reghdfe empl robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl85
reghdfe empl                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl86
reghdfe empl robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl87
reghdfe empl robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl88
reghdfe empl                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl89

qui estimates restore repl81
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster replace word tex ctitle(Employed) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl82
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl83
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl84
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl85
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl86
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl87
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl88
qui outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl89
	outreg2 using "Robots and Workers\Results\Worker_hetero_emplext", label aster append word tex ctitle(Employed) nocon				
	

////////////////////////////////////////////////////////////////////////////////
////////////////6) WORKER-LEVEL ANALYSIS EXTENDED EVENT STUDIES/////////////////
////////////////////////////////////////////////////////////////////////////////			

* Create Figure C1

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	sort rinpersoon year

	// Do the other selections 
	keep if hwweight == 1
	sort rinpersoon year
	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid year)
	keep if include==1 & include_post2009==1
	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee ==.)
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)
	egen industry_ifrXyear = group(industry_ifr year)

	// Obtain event study variables
	merge m:1 beid year using "Robots and Workers\Data\BEID_PS_20002020.dta", nogen keep(1 3 4 5) keepusing(robots*)
	g robots_gttmin5 = 1-robots_tmin5-robots_tmin4-robots_tmin3-robots_tmin2-robots_tmin1-robots
	g robots_gttmin4 = 1-robots_tmin4-robots_tmin3-robots_tmin2-robots_tmin1-robots
	g robots_gttmin3 = 1-robots_tmin3-robots_tmin2-robots_tmin1-robots

reghdfe loghwage robots_gttmin4 robots_tmin4 robots_tmin3 robots_tmin1 robots_t0 robots_t1 robots_gte2 $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl2
reghdfe loghwage robots_gttmin4 robots_tmin4 robots_tmin3 robots_tmin1 robots_t0 robots_t1 robots_gte2 $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl3

// Export output
global sheetname EventStudies_workers  

//// Baseline results
putexcel set "Robots and Workers\Results\Figures.xlsx", modify sheet($sheetname)
putexcel A1 = "N=1,131,970"
putexcel A4 = "Worker level results"
putexcel A5 = "Years before/after robot adoption"
putexcel A6 = -5
putexcel A7 = -5
putexcel A8 = -4
putexcel A9 = -4
putexcel A10 = -3
putexcel A11 = -3
putexcel A12 = -2
putexcel A13 = -2
putexcel A14 = -1
putexcel A15 = -1
putexcel A16 = 0
putexcel A17 = 0
putexcel A18 = 1	
putexcel A19 = 1	
putexcel A20 = 2	
putexcel A21 = 2

local i = 1
foreach spec in 2 3 {
	estimates restore repl`spec'
	local letter = char(65+`i')
	putexcel `letter'5 = "Specification `spec'"
	putexcel `letter'6 = _b[robots_gttmin4]
	putexcel `letter'7 = _b[robots_gttmin4]
	putexcel `letter'8 = _b[robots_tmin4]
	putexcel `letter'9 = _b[robots_tmin4]
	putexcel `letter'10 = _b[robots_tmin3]
	putexcel `letter'11 = _b[robots_tmin3]
	putexcel `letter'12 = 0
	putexcel `letter'13 = 0
	putexcel `letter'14 = _b[robots_tmin1]
	putexcel `letter'15 = _b[robots_tmin1]
	putexcel `letter'16 = _b[robots_t0]
	putexcel `letter'17 = _b[robots_t0]
	putexcel `letter'18 = _b[robots_t1]
	putexcel `letter'19 = _b[robots_t1]
	putexcel `letter'20 = _b[robots_gte2]	
	putexcel `letter'21 = _b[robots_gte2]	
	local letter = char(66+`i')
	putexcel `letter'6 = _b[robots_gttmin4]-1.96*_se[robots_gttmin4]
	putexcel `letter'7 = _b[robots_gttmin4]+1.96*_se[robots_gttmin4]
	putexcel `letter'8 = _b[robots_tmin4]-1.96*_se[robots_tmin4]
	putexcel `letter'9 = _b[robots_tmin4]+1.96*_se[robots_tmin4]
	putexcel `letter'10 = _b[robots_tmin3]-1.96*_se[robots_tmin3]
	putexcel `letter'11 = _b[robots_tmin3]+1.96*_se[robots_tmin3]
	putexcel `letter'12 = 0
	putexcel `letter'13 = 0
	putexcel `letter'14 = _b[robots_tmin1]-1.96*_se[robots_tmin1]
	putexcel `letter'15 = _b[robots_tmin1]+1.96*_se[robots_tmin1]
	putexcel `letter'16 = _b[robots_t0]-1.96*_se[robots_t0]
	putexcel `letter'17 = _b[robots_t0]+1.96*_se[robots_t0]
	putexcel `letter'18 = _b[robots_t1]-1.96*_se[robots_t1]
	putexcel `letter'19 = _b[robots_t1]+1.96*_se[robots_t1]
	putexcel `letter'20 = _b[robots_gte2]-1.96*_se[robots_gte2]
	putexcel `letter'21 = _b[robots_gte2]+1.96*_se[robots_gte2]
	local i = `i'+2
}	
	
		

////////////////////////////////////////////////////////////////////////////////
///////////////////7) WORKER-LEVEL ANALYSIS -- LONG-DIFFERENCES/////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table C1

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	keep if hwweight == 1
	sort rinpersoon year
	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid year)
	keep if include==1 & include_post2009==1
	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee ==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)
	egen industry_ifrXyear = group(industry_ifr year)
	
	// Long differences
reghdfe loghwage robots [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl1
reghdfe loghwage robots $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl2
reghdfe loghwage robots $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl3
reghdfe empl robots [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl4
reghdfe empl robots $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl5
reghdfe empl robots $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl6	
reghdfe loghoursworked robots [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl7
reghdfe loghoursworked robots $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl8
reghdfe loghoursworked robots $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & (year==2009 | year==2020)
	estimates store ld_repl9		

qui estimates restore ld_repl1
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore ld_repl2
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore ld_repl3
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore ld_repl4
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Employed) nocon
qui estimates restore ld_repl5
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Employed) nocon
qui estimates restore ld_repl6
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Employed) nocon
qui estimates restore ld_repl7
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore ld_repl8
qui outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore ld_repl9
    outreg2 using "Robots and Workers\Results\Worker_longdifferences", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	

	
////////////////////////////////////////////////////////////////////////////////
///////////8) WORKER-LEVEL ANALYSIS -- BY DEMOGRPAPHIC CHARACTERISTICS /////////
////////////////////////////////////////////////////////////////////////////////		

* Create Table C2

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	*keep if loghwage!=. & robots !=.
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

	g robotsXyoungworker = robots*(age<55)
	g robotsXoldworker = robots*(age>=55)
	g robotsXnonmigrant = robots*(migrant!=1 & migrant_2ndgen!=1)
	g robotsXmigrant = robots*(migrant==1 | migrant_2ndgen==1)
	g robotsXnonmigrant1 = robots*(migrant!=1)
	g robotsXmigrant1 = robots*(migrant==1)
	g robotsXmale = robots*(male==1)
	g robotsXfemale = robots*(male==0)		
		
reghdfe loghwage robotsXyoungworker robotsXoldworker $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl41
reghdfe loghwage robotsXyoungworker robotsXoldworker $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl42
reghdfe loghwage                    robotsXoldworker $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl43	
reghdfe loghwage robotsXnonmigrant robotsXmigrant $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl44
reghdfe loghwage robotsXnonmigrant robotsXmigrant $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl45
reghdfe loghwage                   robotsXmigrant $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl46
reghdfe loghwage robotsXmale robotsXfemale $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl47
reghdfe loghwage robotsXmale robotsXfemale $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl48
reghdfe loghwage            robotsXfemale $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl49

	
	qui estimates restore repl41
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
	qui estimates restore repl42
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore repl43
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore repl44
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore repl45
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore repl46
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore repl47
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore repl48
	qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore repl49
	    outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_interactions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon	

		

////////////////////////////////////////////////////////////////////////////////
/////////////////9) WORKER-LEVEL ANALYSIS -- ADD INTERACTION EFFECTS////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table C3

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen

	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

	g robotsXoldworker = robots*(age>=55)
	g robotsXmigrant = robots*(migrant==1 | migrant_2ndgen==1)
	g robotsXmale = robots*(male==1)
	g robotsXfemale = robots*(male==0)

reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $zvar $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl1
reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $zvar $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl2
reghdfe loghwage                   robotsXmeffect meffect $zvar $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl3	
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $zvar $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl4
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $zvar $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl5
reghdfe loghwage                robotsXrepl replaceable $zvar $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl6
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $zvar $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl7
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $zvar $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl8
reghdfe loghwage                robotsXlowskilled lowskilled $zvar $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl9

	
qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl9
	outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_addinteractions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon	

		
	

////////////////////////////////////////////////////////////////////////////////
/////10) WORKER-LEVEL ANALYSIS BY WORKER TYPE WITH NARROWER MATCHINGWINDOW//////
////////////////////////////////////////////////////////////////////////////////			
	
* Create Table C4 
	
use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 1 & include==1 & include_post2009==1   // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl1
reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl2
reghdfe loghwage                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl3	
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl4
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl5
reghdfe loghwage                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl6
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl7
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl8
reghdfe loghwage                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl9
		
qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl9
    outreg2 using "Robots and Workers\Results\Worker_hetero_window_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon	


* Create Table C5, Panel A
		
reghdfe loghoursworked robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl61
reghdfe loghoursworked robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl62
reghdfe loghoursworked                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl63	
reghdfe loghoursworked robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl64
reghdfe loghoursworked robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl65
reghdfe loghoursworked                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl66
reghdfe loghoursworked robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl67
reghdfe loghoursworked robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl68
reghdfe loghoursworked                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl69

qui estimates restore repl61
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster replace word tex ctitle(Hours worked \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl62
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl63
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl64
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl65
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl66
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl67
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl68
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl69
    outreg2 using "Robots and Workers\Results\Worker_hetero_window_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	

* Create Table C5, Panel B

reghdfe empl robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl81
reghdfe empl robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl82
reghdfe empl                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl83	
reghdfe empl robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl84
reghdfe empl robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl85
reghdfe empl                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl86
reghdfe empl robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl87
reghdfe empl robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl88
reghdfe empl                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl89

qui estimates restore repl81
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster replace word tex ctitle(Employed) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl82
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl83
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl84
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl85
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl86
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl87
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl88
qui outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl89
    outreg2 using "Robots and Workers\Results\Worker_hetero_window_emplext", label aster append word tex ctitle(Employed) nocon	
			
	
	
////////////////////////////////////////////////////////////////////////////////
////////11) WORKER-LEVEL ANALYSIS BY WORKER TYPE -- NO OCCUPATON CHANGE/////////
////////////////////////////////////////////////////////////////////////////////			
		
* Create Table C6

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	merge m:1 rinpersoon year using "Robots and Workers\Data\Workers_PS_LFS_jobchanges.dta", nogen keep(1 3)	
	merge m:1 beid year using "Robots and Workers\Data\BEID_PS_20002020.dta", nogen keep(1 3 4 5) keepusing(robots_t0 robots_t1 robots_t2 robots_t3 robots_t4 robots_gte5)
		g robotsyear = 9999
		replace robotsyear = year if (robots_t0==1)
		replace robotsyear = year-1 if (robots_t1==1)
		replace robotsyear = year-2 if (robots_t2==1)
		replace robotsyear = year-3 if (robots_t3==1)
		replace robotsyear = year-4 if (robots_t4==1)
		replace robotsyear = year-25 if (robots_gte5==1)

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

	g robotsfirmXmeffect = robotsfirm*meffect
	g robotsfirmXrepl = robotsfirm*replaceable
	g robotsfirmXlowskilled = robotsfirm*lowskilled

	
reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl1
reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl2
reghdfe loghwage                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl3	
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl4
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl5
reghdfe loghwage                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl6
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl7
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear
	estimates store repl8
reghdfe loghwage                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1 & lfsyear<robotsyear	
	estimates store repl9
	
qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchangee", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl9
	outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_excloccchange", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon	
	
	
////////////////////////////////////////////////////////////////////////////////
//////////12) WORKER-LEVEL ANALYSIS BY WORKER TYPE -- EXISTING WORKERS//////////
////////////////////////////////////////////////////////////////////////////////			

* Create Table C7
		
use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	merge m:1 rinpersoon year using "Robots and Workers\Data\Workers_PS_LFS_jobchanges.dta", nogen keep(1 3)	
	merge m:1 beid year using "Robots and Workers\Data\BEID_PS_20002020.dta", nogen keep(1 3 4 5) keepusing(robots_t0 robots_t1 robots_t2 robots_t3 robots_t4 robots_gte5)
		g robotsyear = 9999
		replace robotsyear = year if (robots_t0==1)
		replace robotsyear = year-1 if (robots_t1==1)
		replace robotsyear = year-2 if (robots_t2==1)
		replace robotsyear = year-3 if (robots_t3==1)
		replace robotsyear = year-4 if (robots_t4==1)
		replace robotsyear = year-25 if (robots_gte5==1)

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

	g robotsfirmXmeffect = robotsfirm*meffect
	g robotsfirmXrepl = robotsfirm*replaceable
	g robotsfirmXlowskilled = robotsfirm*lowskilled

	capture drop mover 
	g mover = max(newhire,leaver)

	g robotsXnonmeffectXmover = robots*(1-meffect)*mover
	g robotsXnonmeffectXstayer = robots*(1-meffect)*(1-mover)
	g robotsXmeffectXmover = robots*meffect*mover
	g robotsXmeffectXstayer = robots*meffect*(1-mover)

	g robotsXnonreplXmover = robots*(1-replaceable)*mover
	g robotsXnonreplXstayer = robots*(1-replaceable)*(1-mover)
	g robotsXreplXmover = robots*replaceable*mover
	g robotsXreplXstayer = robots*replaceable*(1-mover)

	g robotsXnonlowskilledXmover = robots*(1-lowskilled)*mover
	g robotsXnonlowskilledXstayer = robots*(1-lowskilled)*(1-mover)
	g robotsXlowskilledXmover = robots*lowskilled*mover
	g robotsXlowskilledXstayer = robots*lowskilled*(1-mover)

	g moverXmeffect = mover*meffect
	g moverXreplaceable = mover*replaceable
	g moverXlowskilled = mover*lowskilled


reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl1
reghdfe loghwage robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl2
reghdfe loghwage                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl3	
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl4
reghdfe loghwage robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl5
reghdfe loghwage                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl6
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl7
reghdfe loghwage robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl8
reghdfe loghwage                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1 & mover==0
	estimates store repl9
		
qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl9
    outreg2 using "Robots and Workers\Results\Worker_hetero_hwage_stayers", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon	


	
////////////////////////////////////////////////////////////////////////////////
////////////////////////////13) JOB MOBILITY BETWEEN FIRMS//////////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table C8

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)

	merge m:1 rinpersoon year using "Robots and Workers\Data\Workers_PS_LFS_jobchanges.dta", nogen keep(1 3)	
	foreach var in Δjc_withinfirm Δjc_betweenfirm {
		replace `var' = . if hwage == .
	}

ppmlhdfe Δjc_betweenfirm robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl1
ppmlhdfe Δjc_betweenfirm robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl2
ppmlhdfe Δjc_betweenfirm                   robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl3	
ppmlhdfe Δjc_betweenfirm robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl4
ppmlhdfe Δjc_betweenfirm robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl5
ppmlhdfe Δjc_betweenfirm                robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl6
ppmlhdfe Δjc_betweenfirm robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl7
ppmlhdfe Δjc_betweenfirm robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl8
ppmlhdfe Δjc_betweenfirm                robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl9
		
qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster replace word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p)) addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).)
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
qui estimates restore repl9
	outreg2 using "Robots and Workers\Results\Worker_hetero_jobchangebetween", label aster append word tex ctitle(Job change) nocon addstat(Pseudo-R2,e(r2_p))
		
		
////////////////////////////////////////////////////////////////////////////////
///////////14) WORKER-LEVEL ANALYSIS BY MEDIUM AND HIGH SKILLED WORKERS/////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table C9

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)		
		
			
reghdfe loghwage robotsXhighskilled robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl10
reghdfe loghwage robotsXhighskilled robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl11
reghdfe loghwage robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl12
reghdfe loghoursworked robotsXhighskilled robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl70
reghdfe loghoursworked robotsXhighskilled robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl71
reghdfe loghoursworked robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl72
reghdfe empl robotsXhighskilled robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl90
reghdfe empl robotsXhighskilled robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl91
reghdfe empl robotsXmedskilled robotsXlowskilled medskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl92

qui estimates restore repl10
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl11
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl12
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore repl70
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl71
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl72
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
qui estimates restore repl90
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl91
qui outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Employed) nocon
qui estimates restore repl92
	outreg2 using "Robots and Workers\Results\Worker_medhighskilled", label aster append word tex ctitle(Employed) nocon	
			
		
	
////////////////////////////////////////////////////////////////////////////////
//////////////15) WORKER-LEVEL ANALYSIS BY WORKER TYPE -- EARNINGS//////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table C10

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect routine) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid fyear)
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1    // Set LFS window

	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)

	g robotsXrepl = robots*replaceable
	g robotsXnonrepl = robots*(1-replaceable)
	g robotsXmeffect = robots*meffect
	g robotsXnonmeffect = robots*(1-meffect)
	g robotsXlowskilled = robots*lowskilled
	g robotsXnonlowskilled = robots*(1-lowskilled)
	g robotsXmedskilled = robots*medskilled
	g robotsXnonmedskilled = robots*(1-medskilled)
	g robotsXhighskilled = robots*(1-lowmedskilled)
	
reghdfe logearnings robotsXnonmeffect robotsXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl41
reghdfe logearnings robotsXnonmeffect robotsXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl42
reghdfe logearnings robotsXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl43	
reghdfe logearnings robotsXnonrepl robotsXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl44
reghdfe logearnings robotsXnonrepl robotsXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl45
reghdfe logearnings robotsXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl46
reghdfe logearnings robotsXnonlowskilled robotsXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl47
reghdfe logearnings robotsXnonlowskilled robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl48
reghdfe logearnings robotsXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl49
	
qui estimates restore repl41
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster replace word tex ctitle(Income \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl42
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon
qui estimates restore repl43
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon
qui estimates restore repl44
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon
qui estimates restore repl45
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon
qui estimates restore repl46
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon
qui estimates restore repl47
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon
qui estimates restore repl48
qui outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon
qui estimates restore repl49
	outreg2 using "Robots and Workers\Results\Worker_hetero_earnings", label aster append word tex ctitle(Income \textit{(log)}) nocon			
	
				
		
